package cn.qidd.keep.member.controller;


import cn.qidd.keep.domain.core.Result;
import cn.qidd.keep.domain.core.dict.MemberStatus;
import cn.qidd.keep.domain.document.Member;
import cn.qidd.keep.domain.document.Product;
import cn.qidd.keep.domain.web.BaseController;
import cn.qidd.keep.domain.web.UserVO;
import cn.qidd.keep.member.feignclient.ProductServiceClient;
import cn.qidd.keep.member.model.AddMemberParams;
import cn.qidd.keep.member.model.SearchMemberParam;
import cn.qidd.keep.member.model.UpdateMemberParams;
import cn.qidd.keep.member.repository.MemberRepository;
import cn.qidd.keep.member.util.MemberUtil;
import com.querydsl.core.BooleanBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Date;

import static cn.qidd.keep.domain.core.Constants.TOKEN;

@RestController
@RequestMapping("/api/member")
@Api(value = "", description = "会员相关操作")
public class MemberController extends BaseController {

    @Resource
    private MemberRepository memberRepository;
    @Autowired
    private ProductServiceClient productServiceClient;


    @PostMapping("/addMember")
    @ApiOperation(value = "添加会员", notes = "添加会员")
    public Result addMember(@ApiIgnore @RequestHeader(TOKEN) String token, @Validated @RequestBody AddMemberParams params) {
        logger.debug("Rest request add member {}", params);
        UserVO userVO = getUserByToken(token);
        // 查询产品
        Member member = new Member();
        BeanUtils.copyProperties(params, member);
        member.setMemberNo(MemberUtil.createNo());
        Product product = productServiceClient.getProduct(params.getProductId());
        member.setProductId(product.getId());
        member.setProductName(product.getProductName());
        member.setProductContent(product.getProductContent());
        member.setProductPrice(product.getProductPrice());
        member.setLeaveCount(params.getCount());
        member.setMemberStatus(MemberStatus.NORMAL);
        member.setOperatorName(userVO.getUserName());
        member.setOperatorRealName(userVO.getRealName());
        member.setOperatorTime(new Date());
        memberRepository.save(member);
        return new Result(Result.Status.SUCCESS, "添加成功");
    }

    @PutMapping("/updateMember")
    @ApiOperation(value = "编辑会员", notes = "编辑会员")
    public Result updateMember(@ApiIgnore @RequestHeader(TOKEN) String token, @Validated @RequestBody UpdateMemberParams params) {
        logger.debug("Rest request update member {}", params);
        UserVO userVO = getUserByToken(token);
        Member member = memberRepository.findById(params.getId()).orElseThrow(RuntimeException::new);
        member.setName(params.getName());
        member.setMobile(params.getMobile());
        member.setOperatorName(userVO.getUserName());
        member.setOperatorRealName(userVO.getRealName());
        member.setOperatorTime(new Date());
        memberRepository.save(member);
        return new Result(Result.Status.SUCCESS, "修改成功");
    }

    @GetMapping("/findAllListByPage")
    @ApiOperation(value = "查询会员", notes = "查询会员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", defaultValue = "0", dataType = "integer", paramType = "query",
                    value = "页数 (1..N)"),
            @ApiImplicitParam(name = "pageSize", defaultValue = "10", dataType = "integer", paramType = "query",
                    value = "每页大小"),
            @ApiImplicitParam(name = "sort", dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名1 asc,属性名2 desc. ")
    })
    public Result findAllListByPage(@ApiIgnore @RequestHeader(TOKEN) String token,
                                    @ApiIgnore Pageable pageable,
                                    SearchMemberParam params) {
        logger.debug("Rest request find all member by page {}", params);
        getUserByToken(token);
        BooleanBuilder builder = params.generateSearchCondition();
        Page<Member> all = memberRepository.findAll(builder, pageable);
        return new Result<>(Result.Status.SUCCESS, "", all);
    }

}
